
def initial_session(user,request):
    permissions = user.roles.all().values("permissions__url", "permissions__permissiongroup_id", "permissions__action").distinct()

    permission_dict = {}
    for item in permissions:
        gid = item.get('permissions__permissiongroup_id')

        if not gid in permission_dict:
            permission_dict[gid] = {
                "urls": [item["permissions__url"], ],
                "actions": [item["permissions__action"], ]
            }
        else:
            permission_dict[gid]["urls"].append(item["permissions__url"])
            permission_dict[gid]["actions"].append(item["permissions__action"])
    request.session['permission_dict'] = permission_dict

    # 注册菜单权限
    permissions = user.roles.all().values("permissions__url", "permissions__action", "permissions__permissiongroup__title").distinct()

    menu_permission_list = []
    for item in permissions:
        if item["permissions__action"] == "list":
            menu_permission_list.append((item["permissions__url"], item["permissions__permissiongroup__title"]))

    request.session["menu_permission_list"] = menu_permission_list


